<?php

/**
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */
if ($_POST) {
    require_once(dirname(__FILE__) . '/../config/config.inc.php');

    if ($_POST['id_product']) {

        if (Util::is_email($_POST['email'])) {
            $data = array(
                'id_product' => $_POST['id_product'],
                'client' => $_POST['name'],
                'email' => $_POST['email'],
                'phone' => preg_replace('/\D/s', '', $_POST['phone']),
                'message' => $_POST['message'],
                'read' => 0
            );
            if (DbSet::products_proposals($data)) {
                $owner = DbGet::products_owners($_POST['id_product']);
                if ($owner->type=='F') {
                    $data = DbGet::customer($owner->id_client);
                    $addr = $data->a_email;
                } elseif ($owner->type=='J') {
                    $data = DbGet::enterprise($owner->id_client);
                    $addr = $data->a_email;
                }
                $subject = 'Proposta de Produto - Goiautos';

                $message = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">' . "\n";
                $message .= '<HTML>' . "\n";
                $message .= '    <HEAD>' . "\n";
                $message .= '        <META HTTP-EQUIV=\"CONTENT-TYPE\" CONTENT=\"text/html; charset=utf-8\">' . "\n";
                $message .= '    </HEAD>' . "\n";
                $message .= '    <BODY LANG=\"pt-BR\" DIR=\"LTR\">' . "\n";
                $message .= '        <p>Nome: ' . $_POST['name'] . '</p>' . "\n";
                $message .= '        <p>Email: ' . $_POST['email'] . '</p>' . "\n";
                $message .= '        <p>Telefone: ' . $_POST['phone'] . '</p>' . "\n";
                $message .= '        <p>Mensagem:<br/>' . $_POST['message'] . '</p>' . "\n";
                $message .= '    </BODY>' . "\n";
                $message .= '</HTML>' . "\n";

                $headers = 'MIME-Version: 1.1' . "\n";
                $headers .= 'Content-type: text/html; charset=utf-8' . "\n";
                $headers .= 'Content-Transfer-Encoding: 8bit' . "\n";
                $headers .= 'From: "' . $_POST['name'] . '" <' . $_POST['email'] . '>' . "\n";
                $headers .= 'Reply-To: "' . $_POST['name'] . '" <' . $_POST['email'] . '>' . "\n";
                $headers .= 'Return-Path: ' . $_POST['email'] . "\n";

                @mail($addr, $subject, $message, $headers);
                echo 'OK';
            }
            else
                echo 'ERROR';
        }
    } else {
        if (Util::is_email($_POST['email'])) {
            $data = array(
                'id_vehicle' => $_POST['id'],
                'client' => $_POST['name'],
                'email' => $_POST['email'],
                'phone' => preg_replace('/\D/s', '', $_POST['phone']),
                'message' => $_POST['message'],
                'read' => 0
            );
            if (DbSet::proposals($data)) {
                if ($data = DbGet::customer_vehicle($_POST['id'])) {
                    $data = DbGet::customer($data->c_id);
                    $addr = $data->a_email;
                } elseif ($data = DbGet::enterprise_vehicle($_POST['id'])) {
                    $data = DbGet::enterprise($data->e_id);
                    $addr = $data->a_email;
                }
                $subject = 'Proposta - Goiautos';

                $message = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">' . "\n";
                $message .= '<HTML>' . "\n";
                $message .= '    <HEAD>' . "\n";
                $message .= '        <META HTTP-EQUIV=\"CONTENT-TYPE\" CONTENT=\"text/html; charset=utf-8\">' . "\n";
                $message .= '    </HEAD>' . "\n";
                $message .= '    <BODY LANG=\"pt-BR\" DIR=\"LTR\">' . "\n";
                $message .= '        <p>Nome: ' . $_POST['name'] . '</p>' . "\n";
                $message .= '        <p>Email: ' . $_POST['email'] . '</p>' . "\n";
                $message .= '        <p>Telefone: ' . $_POST['phone'] . '</p>' . "\n";
                $message .= '        <p>Mensagem:<br/>' . $_POST['message'] . '</p>' . "\n";
                $message .= '    </BODY>' . "\n";
                $message .= '</HTML>' . "\n";

                $headers = 'MIME-Version: 1.1' . "\n";
                $headers .= 'Content-type: text/html; charset=utf-8' . "\n";
                $headers .= 'Content-Transfer-Encoding: 8bit' . "\n";
                $headers .= 'From: "' . $_POST['name'] . '" <' . $_POST['email'] . '>' . "\n";
                $headers .= 'Reply-To: "' . $_POST['name'] . '" <' . $_POST['email'] . '>' . "\n";
                $headers .= 'Return-Path: ' . $_POST['email'] . "\n";

                @mail($addr, $subject, $message, $headers);
                echo 'OK';
            }
            else
                echo 'ERROR';
        }
    }
}